package dao

import (
	"gitee.com/k-ayaka/et-userserver/po"
	"gitee.com/k-ayaka/std-go/dao"
)

func CreateUser(name string, password string, email string, tel int) *po.User {
	user := po.User{
		Name:     name,
		Password: password,
		Profile:  "default.jpg",
		Email:    email,
		Tel:      tel,
	}
	if err := dao.DB.Table("users").Save(&user).Error; err != nil {
		return nil
	}
	return &user
}

func DeleteUser(id int) bool {
	err := dao.DB.Table("users").Delete(&po.User{}, id).Error
	return err == nil
}

func UpdateUser(user *po.User) bool {
	err := dao.DB.Table("users").Model(&user).Updates(user).Error
	return err == nil
}

func SelectUser(id int) *po.User {
	var user po.User
	if dao.DB.Table("users").Find(&user, "id=?", id).RecordNotFound() {
		return nil
	}
	return &user
}
